qui(function (require, exports, module) {
    Program.TabControl = (function(){
        Array.prototype.remove = function () {
            var what, a = arguments, L = a.length, ax;
            while (L && this.length) {
                what = a[--L];
                while ((ax = this.indexOf(what)) !== -1) {
                    this.splice(ax, 1);
                }
            }
            return this;
        };

        function initOperationTabControlArea(d) {
            var data = null;
            if (d === undefined || d == "") {
                data = JSON.parse(Program.Config.defaultOperationTabControl);
            } else {
                data = JSON.parse(d);
            }

            var area = $('#operationTabControlArea');

            var content = [];

            $.each(data, function (i, e) {
                content.push(makeUpTab(e));
            });

            area.html(content.join(""));

            area.find('li:first-child button.up').hide();
            area.find('li:last-child button.down').hide();

            opSetDisplayByRelation();

            $.each(area.find('li'), function (i, e) {
                opBindDefaultClick(e);
                opBindDisplayClick(e);
            })
        }

        function opHideUpDownBtn() {
            var area = $('#operationTabControlArea');

            area.find('li:first-child button.up').hide();
            area.find('li:last-child button.down').hide();
        }

        function makeUpTab(e) {
            var htmlArr = [];
            switch (parseInt(e.type)) {
                case 0:
                    htmlArr.push('<li data-type="' + e.type + '"><div class="operationDiv"><div class="control-group">');
                    htmlArr.push('<select style="width:90px;" disabled="disabled"><option value="simple-video">聊天室</option></select>');
                    htmlArr.push('<input type="text" name="op_display_name" placeholder="" value="' + e.display_name + '" style="margin-left: 4px;"></div>');
                    htmlArr.push('<div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">是否显示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_chat_room" value="1"' + (e.is_enable == 1 ? "checked" : "") + ' disabled>是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_chat_room" value="0"' + (e.is_enable == 0 ? "checked" : "") + ' disabled>否</label>');
                    htmlArr.push('<label class="inline radio" style="color: #bfbfbf">聊天室开关控制</label>');
                    htmlArr.push('</div></div><div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">默认展示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_chat_room" value="1"' + (e.is_default == 1 ? "checked" : "") + ' >是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_chat_room" value="0"' + (e.is_default == 0 ? "checked" : "") + ' >否</label>');
                    htmlArr.push('<button type="button" class="btn add blue-btn down" onclick="Program.TabControl.opTabMove(this, 1)"> 下移</button>');
                    htmlArr.push('<button type="button" class="btn add blue-btn up" onclick="Program.TabControl.opTabMove(this, 0)"> 上移</button>');
                    htmlArr.push('</div></div></div></li>');
                    break;
                case 1:
                    htmlArr.push('<li data-type="' + e.type + '"><div class="operationDiv"><div class="control-group">');
                    htmlArr.push('<select style="width:90px;" disabled="disabled"><option value="simple-video">看更多</option></select>');
                    htmlArr.push('<input type="text" name="op_display_name" placeholder="" value="' + e.display_name + '" style="margin-left: 4px;"></div>');
                    htmlArr.push('<div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">是否显示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_more" value="1"' + (e.is_enable == 1 ? "checked" : "") + '>是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_more" value="0"' + (e.is_enable == 0 ? "checked" : "") + '>否</label>');
                    htmlArr.push('</div></div><div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">默认展示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_more" value="1"' + (e.is_default == 1 ? "checked" : "") + ' >是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_more" value="0"' + (e.is_default == 0 ? "checked" : "") + ' >否</label>');
                    htmlArr.push('<button type="button" class="btn add blue-btn down" onclick="Program.TabControl.opTabMove(this, 1)"> 下移</button>');
                    htmlArr.push('<button type="button" class="btn add blue-btn up" onclick="Program.TabControl.opTabMove(this, 0)"> 上移</button>');
                    htmlArr.push('</div></div></div></li>');
                    break;
                case 2:
                    htmlArr.push('<li data-type="' + e.type + '"><div class="operationDiv"><div class="control-group">');
                    htmlArr.push('<select style="width:90px;" disabled="disabled"><option value="simple-video">排行榜</option></select>');
                    htmlArr.push('<input type="text" name="op_display_name" placeholder="" value="' + e.display_name + '" style="margin-left: 4px;" id="input_tab_list"></div>');
                    htmlArr.push('<div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">是否显示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_list" value="1"' + (e.is_enable == 1 ? "checked" : "") + ' disabled>是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_list" value="0"' + (e.is_enable == 0 ? "checked" : "") + ' disabled>否</label>');
                    htmlArr.push('<label class="inline radio" style="color: #bfbfbf">道具开关控制</label>');
                    htmlArr.push('</div></div><div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">默认展示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_list" value="1"' + (e.is_default == 1 ? "checked" : "") + ' >是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_list" value="0"' + (e.is_default == 0 ? "checked" : "") + ' >否</label>');
                    htmlArr.push('<button type="button" class="btn add blue-btn down" onclick="Program.TabControl.opTabMove(this, 1)"> 下移</button>');
                    htmlArr.push('<button type="button" class="btn add blue-btn up" onclick="Program.TabControl.opTabMove(this, 0)"> 上移</button>');
                    htmlArr.push('</div></div></div></li>');
                    break;
                case 3:
                    htmlArr.push('<li data-type="' + e.type + '" data-id="' + e.id + '"><div class="operationDiv"><div class="control-group">');
                    htmlArr.push('<select style="width:90px;" disabled="disabled"><option value="simple-video">活动页</option></select>');
                    htmlArr.push('<input type="text" name="op_display_name" placeholder="" value="' + e.display_name + '" style="margin-left: 4px;">');
                    htmlArr.push('<button type="button" class="btn add blue-btn" style="float: right;" onclick="Program.TabControl.opDelete(this,' + e.id + ');"> 删除</button></div>')
                    htmlArr.push('<div class="control-group">');
                    htmlArr.push('<label class="col-xs-2 control-label" style="text-align: left;width: 80px;margin-left:4px;">活动地址：</label>');
                    htmlArr.push('<input type="text" name="text_activity' + e.id + '" placeholder="" value="' + e.url + '"></div>');
                    htmlArr.push('<div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">是否显示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_activity' + e.id + '" value="1"' + (e.is_enable == 1 ? "checked" : "") + '>是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_activity' + e.id + '" value="0"' + (e.is_enable == 0 ? "checked" : "") + '>否</label>');
                    htmlArr.push('</div></div><div class="control-group"><label class="col-xs-2 control-label" style="width:60px;margin-left:4px;">默认展示：</label>');
                    htmlArr.push('<div class="controls" style="margin-left: 0px;">');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_activity' + e.id + '" value="1"' + (e.is_default == 1 ? "checked" : "") + ' >是</label>');
                    htmlArr.push('<label class="radio inline"><input type="radio" name="radio_default_activity' + e.id + '" value="0"' + (e.is_default == 0 ? "checked" : "") + ' >否</label>');
                    htmlArr.push('<button type="button" class="btn add blue-btn down" onclick="Program.TabControl.opTabMove(this, 1)"> 下移</button>');
                    htmlArr.push('<button type="button" class="btn add blue-btn up" onclick="Program.TabControl.opTabMove(this, 0)"> 上移</button>');
                    htmlArr.push('</div></div></div></li>');

                    Program.Config.opActivityNums.remove(e.id + "");
                    break;
                default:
                    break;
            }

            return htmlArr.join("");
        }

        function opTabMove(element, type) {
            var e = $(element);
            var p = e.closest('li');

            // 0: up, 1: down
            if (type == 0) {
                var prev = p.prev();
                if (prev.length) {
                    prev.insertAfter(p);

                    if (p.prev().length == 0) {
                        p.find("button.up").hide();
                        p.find('button.down').show();
                    } else {
                        p.find("button.up").show();
                        p.find('button.down').show();
                    }

                    if (prev.next().length == 0) {
                        prev.find("button.up").show();
                        prev.find("button.down").hide();
                    } else {
                        prev.find("button.up").show();
                        prev.find("button.down").show();
                    }

                }
            } else if (type == 1) {
                var next = p.next();
                if (next.length) {
                    next.insertBefore(p);

                    if (p.next().length == 0) {
                        p.find("button.down").hide();
                        p.find("button.up").show();
                    } else {
                        p.find("button.down").show();
                        p.find("button.up").show();
                    }
                    if (next.prev().length == 0) {
                        next.find("button.down").show();
                        next.find("button.up").hide();
                    } else {
                        next.find("button.down").show();
                        next.find("button.up").show();
                    }
                }
            }
        }

        function opAddActivity() {
            var area = $('#operationTabControlArea');

            var lis = area.find('li');
            if (lis.length >= 6) {
                showTips({ content: "添加失败，最多支持3个活动页面。", type: "error" });
                return;
            }

            var opId = Program.Config.opActivityNums[0];
            //Program.Config.opActivityNums.remove(opId);

            var tabStr = makeUpTab({
                'type': '3', 'display_name': '', 'url': '', 'is_enable': '1', 'id': opId, 'is_default': '0'});
            var tab = $.parseHTML(tabStr);


            var last = area.find('li:last-child');
            $(last).find('button.down').show();

            $(last).after(tab);

            $(tab).find('button.down').hide();

            opBindDefaultClick($(tab));
            opBindDisplayClick($(tab));
        }

        function opBindDefaultClick(p) {
            var elements = $(p).find('input[name*=radio_default_]')
            $.each(elements, function (i, e) {
                $(e).bind("click", function (event) {
                    var $curTarget = $(event.currentTarget);
                    if ($curTarget.val() == 1) {
                        opSetDefaultDisableOthers($curTarget);
                    } else {
                        //opSetDefaultFirst();
                    }
                })
            })

        }

        function opBindDisplayClick(p) {
            var elements = $(p).find('input[name*=radio_]')
            $.each(elements, function (i, e) {
                $(e).bind("click", function (event) {
                    var $curTarget = $(event.currentTarget);
                    if ($curTarget.val() == 0) {
                        opSetDefaultByDisplay($(event.currentTarget));
                    } else {
                        //opSetDefaultFirst();
                    }
                })
            })
        }

        function opSetDefaultByDisplay(e) {
            var p = $(e).closest('li');
            p.find('input[name*=radio_default_][value="0"]').prop('checked', true);
        }

        function opSetDefaultDisableOthers(element) {
            var p = $(element).closest('li');
            var lis = p.siblings();

            $.each(lis, function (i, e) {
                $(e).find('input[name*=radio_default_][value="0"]').prop('checked', true);
            })
        }

        function opSetDefaultFirst() {
            var first = $('#operationTabControlArea').find('li:first-child');
            $(first).find('input[name*=radio_default_][value="1"]').prop('checked', true);
        }

        function opDelete(element, id) {
            var e = $(element);
            var p = e.closest('li');


            SystemDialog.queryConfirm("确认删除?", function (flag) {
                if (flag == true) {
                    var isDefault = false;
                    if (p.find('input[name*=radio_default_]:checked').val() == 1) {
                        isDefault = true;
                    }
                    p.remove();
                    Program.Config.opActivityNums.push(id + "");
                    opHideUpDownBtn();
                    if (isDefault) {
                        opSetDefaultFirst();
                    }
                }
            })

        }

        function opSetDisplayByRelation() {
            var chat_enable = $('input:radio[name=chat_enable]:checked').val();
            var daoju_enable = $('input:radio[name=daojuFlag]:checked').val();

            $('input:radio[name=radio_chat_room][value=' + chat_enable + ']').prop("checked", true);
            $('input:radio[name=radio_list][value=' + daoju_enable + ']').prop("checked", true);

            if (chat_enable == 0) {
                opSetDefaultByDisplay($('input:radio[name=radio_chat_room][value=0]'));
            }

            if (daoju_enable == 0) {
                opSetDefaultByDisplay($('input:radio[name=radio_list][value=0]'));
            }
        }

        function opGetData() {
            var area = $('#operationTabControlArea');
            var lis = area.find('li');
            var data = [];
            var error = "";
            $.each(lis, function (i, li) {
                var e = $(li);
                var obj = {};

                switch (parseInt(e.attr('data-type'))) {
                    case 0:
                        obj.type = 0;
                        obj.display_name = e.find('input[name=op_display_name]').val();
                        obj.is_enable = e.find('input:radio[name=radio_chat_room]:checked').val();
                        obj.is_default = e.find('input:radio[name=radio_default_chat_room]:checked').val();

                        break;
                    case 1:
                        obj.type = 1;
                        obj.display_name = e.find('input[name=op_display_name]').val();
                        obj.is_enable = e.find('input:radio[name=radio_more]:checked').val();
                        obj.is_default = e.find('input:radio[name=radio_default_more]:checked').val();
                        break;
                    case 2:
                        obj.type = 2;
                        obj.display_name = e.find('input[name=op_display_name]').val();
                        obj.is_enable = e.find('input:radio[name=radio_list]:checked').val();
                        obj.is_default = e.find('input:radio[name=radio_default_list]:checked').val();
                        break;
                    case 3:
                        obj.type = 3;
                        obj.display_name = e.find('input[name=op_display_name]').val();
                        obj.is_enable = e.find('input:radio[name*=radio_activity]:checked').val();
                        obj.is_default = e.find('input:radio[name*=radio_default_]:checked').val();
                        obj.url = e.find('input[name*=text_activity]').val();
                        obj.id = e.attr('data-id');

                        if (obj.display_name == "" || obj.url == "") {
                            showTips({ content: "活动页名称或地址不能为空", type: "error" });
                            error = "活动页名称或地址不能为空";
                            return "";
                        }
                        break;
                    default:
                        break;
                }

                data.push(obj);
            });

            if (error != "") {
                return "";
            }

            return JSON.stringify(data);

        }

        return {
            opTabMove:opTabMove,
            opGetData:opGetData
        }
    })()


});